/* eslint valid-jsdoc: "off" */

/**
 * @param {Egg.EggAppInfo} appInfo app info
 */
const path = require('path');
module.exports = appInfo => {
  /**
   * built-in config
   * @type {Egg.EggAppConfig}
   **/
  const config = exports = {};

  // use for cookie sign key, should change to your own and keep security
  config.keys = appInfo.name + '_1713685318268_4536';

  config.security =
    {
      csrf: {
        enable: false,
      },
    };
  config.cors = {
    allowMethods: 'GET,HEAD,PUT,POST,DELETE,PATCH,OPTIONS',
    origin: '*',
  };

  // config/config.default.js
  config.bodyParser = {
    enable: true,
    multipart: true,
    jsonLimit: '50mb', // JSON 解析限制
    formLimit: '50mb', // 表单解析限制
    textLimit: '50mb', // 文本解析限制
    formidable: {
      maxFileSize: 200 * 1024 * 1024 // 例如，设置为200MB
    }
  };
  // config/config.default.js
  config.koaBodyOptions = {
    jsonLimit: '10mb', // JSON 解析限制
    formLimit: '10mb', // 表单解析限制
    textLimit: '10mb', // 文本解析限制
    multipart: true, // 启用文件上传
    formidable: {
      maxFileSize: 200 * 1024 * 1024, // 单个文件大小限制为200MB
      // uploadDir: path.join(appInfo.tmpdir, 'web-upload'), // 上传目录
      keepExtensions: true, // 保持文件扩展名
      maxFieldsSize: 2 * 1024 * 1024, // 字段大小限制为2MB
    }
  };
  // add your middleware config here
  const appPort = 18008;
  // const appPort = 28900;
  config.appPort = appPort;
  // add your middleware config heremiddleware
  config.middleware = [ 'recordRequests' ];
  // config.multipart = {
  //   fileSize: '5mb', // 限制文件上传的大小
  //   mode: 'stream', // 使用流模式上传
  // };
  config.multipart = {
    mode: 'file',
    tmpdir: path.join(appInfo.baseDir, 'app/public/uploadFiles'),
    whitelist: (filename) => [ '.jpg', '.jpeg', '.png', '.pdf' ].includes(path.extname(filename)
      .toLowerCase()),
    fileSize: '10mb',
    fields: 10,
    files: 10,
  };
  config.static = {
    prefix: '/',
    dir: path.join(appInfo.baseDir, './public'),
    dynamic: true, // 是否緩存靜態資源
    preload: false, // 啓動項目開啓緩存
    // maxAge: 31536000,
    maxAge: 0, // 緩存時間
    buffer: true, // 是否緩存到内存 默認prod 緩存
  };
  config.cluster = {
    listen: {
      path: '',
      port: appPort,
    },
  };
  config.schedule = {
    directory: [],
  };

  // add your user config here
  const userConfig = {
    // myAppName: 'egg',
  };

  return {
    ...config,
    ...userConfig,
  };
};
